#!/bin/sh
###   Thaw   ###
#
# Freon Linux package manager
# (C) Chris Dorman, 2017-2020 LGPLv2
#
###   END   ####

# Include Freon Linux's config
. /etc/conf.d/main.conf
. /etc/conf.d/status

# Nutrapak root
THAWROOT="/share/thaw"
# Package list
PKGLIST="/share/thaw/package.list"
# Package list name (could change in the future)
PKGLISTNAME="package.list"
# Mirror file
MIRRORFILE="/share/thaw/mirror.txt"
# TCZ mount point
TCZMOUNT="/mnt/pkg"
# Temp path
TMPPATH="/tmp/thaw"
# Installed package dir
INSTALLEDPKG="/share/thaw/installed"

if [ $(id -u) != "0" ]; then
	echo "Error: must be root to operate this command."
	exit 1
fi

# Check Thaw tmp directory
if [ ! -d "$TMPPATH" ]; then
        mkdir $TMPPATH
fi

# Check if any packages are installed, if not fix before break
if [ ! -d "$INSTALLEDPKG" ]; then
	mkdir $INSTALLEDPKG
fi

case $1 in
	get-install ) 
		echo ""
		# check package list
		if [ ! -s $PKGLIST ]; then
			rm $PKGLIST
			echo "Getting package list... "
			wget $(cat $MIRRORFILE)/$PKGLISTNAME -P $THAWROOT
		fi
		
		# check if package is already installed
		if [ -f "$INSTALLEDPKG/$2" ]; then
			echo "${2} is already installed."
			exit 0
		fi

		if [ -f $TMPPATH/$2 ]; then
			rm $TMPPATH/$2
		fi
		
		if [ ! -d $TMPPATH/$2 ]; then
			mkdir $TMPPATH/$2
		fi

		# Check to see if package exists, if so download
		echo "Checking ${2}'s existence... "
		if grep "$2" $PKGLIST > /dev/null
		then
			echo ""
			echo "Downloading ${2}..."
			
			wget $(cat $MIRRORFILE)/$2.tgz -P $TMPPATH/
			wget $(cat $MIRRORFILE)/$2.deps -P $TMPPATH/
			if grep -q "CHTTPD Error" $TMPPATH/$2.deps; then
				rm $TMPPATH/$2.deps
			fi
		else
			echo "${2} was not found in repository."
			exit 1
		fi
		
		# Extract the downloaded file
		echo -n "Extracting package to tmp... "

		tar -xzf $TMPPATH/$2.tgz -C $TMPPATH/$2/
		status
		
		# Examine dependencies for package
		if [ -f "$TMPPATH/$2.deps" ]; then
			i=1
			echo ""
			echo "++++++++++++++++++++++++++++++++++++++++"
			echo "Examining dependencies... "
			echo "++++++++++++++++++++++++++++++++++++++++"
			while read line; do
				if [ -f "$INSTALLEDPKG/$line" ]; then
					echo "$line - INSTALLED"
				else
					echo "$line"
				fi
			done < $TMPPATH/$2.deps
			sleep 1
			echo "++++++++++++++++++++++++++++++++++++++++"
			echo "Installing dependencies..."
			echo "++++++++++++++++++++++++++++++++++++++++"
			while read line; do
				if [ -f "$INSTALLEDPKG/$line" ]; then
					echo "${line} already installed"
				else
					echo "Installing ${line} for ${2}"
					thaw get-install ${line}
				fi
			done < $TMPPATH/$2.deps
			echo "++++++++++++++++++++++++++++++++++++++++"
			
		fi

		echo -n "Installing ${2}..."
		
		cp -a $TMPPATH/$2/* /
		status_serious
		
		if [ -f "$TMPPATH/$2/execute.sh" ]; then
			echo "Running ${2}'s execution script..."
			$TMPPATH/$2/execute.sh
		fi

		echo -n "Cleaning up..."
		if [ -f "/execute.sh" ]; then
                        rm /execute.sh
                fi

                if [ -f "/$2.deps" ]; then
                        rm /$2.deps
                fi

                if [ -f "/$2.tgz" ]; then
                        rm /$2.tgz
                fi

		rm $TMPPATH/$2.tgz
		status
		
		echo -n "Removing temporary files..."
		rm -r $TMPPATH/$2
                status

		echo -n "Marking ${2} in the installation database..."
		touch $INSTALLEDPKG/$2
		status
		
		echo "${2} Installed!"
		echo ""
		;;
	
	update )
		if [ -f $PKGLIST ]; then
			rm $PKGLIST
		fi
		
		echo "Getting package list... "
		wget $(cat $MIRRORFILE)/$PKGLISTNAME -P $THAWROOT
		status_serious
		;;
		
	search )
		if [ ! -s $PKGLIST ]; then
			rm $PKGLIST
			echo "Getting package list... "
			wget $(cat $MIRRORFILE)/$PKGLISTNAME -P $THAWROOT
			status_serious
		fi
		
		echo "Searching results..."
		echo "++++++++++++++++++++++++++++++++++++++++"
		grep -i "$2" $PKGLIST
		echo "++++++++++++++++++++++++++++++++++++++++"
		
		;;

	* )
		echo "Usage: 	
thaw get-install <package> : Install a package
thaw update                : Update system package list
thaw search <query>        : Search for a package in the database
		"
		;;

esac
